<?php
namespace app\mobile\controller;
use think\Db;
use think\Controller;
use think\Session;
use app\mobile\model\Model_index;
use app\api\controller\Index;

class Index extends Controller
{
    private $pub;

    public function _initialize()
    {
        $pub = new Model_index;
        $this->pub = $pub;
    }

    public function index()
    {
        $apii = new Index();
        $api = $apii->callapi();
        $link = $this->pub->link();
        $ses = $this->pub->do_session();
        return view('index',[
            'ses'=>$ses,
            'link'=>$link,
            'api'=>$api,
        ]);
    }

    public function gengxin()
    {
        $link = $this->pub->link();
        $ses = $this->pub->do_session();
        return view('gengxin/gengxin',[
            'ses'=>$ses,
            'link'=>$link,
        ]);
    }

    public function paihang()
    {
        $link = $this->pub->link();
        $ses = $this->pub->do_session();
        return view('paihang/paihang',[
            'ses'=>$ses,
            'link'=>$link,
        ]);
    }

    public function fenlei()
    {
        $link = $this->pub->link();
        $ses = $this->pub->do_session();
        return view('fenlei/fenlei',[
            'ses'=>$ses,
            'link'=>$link,
        ]);
    }

    public function user()
    {
        if (!Session::get('user')) {
            $this->success('抱歉请先登录','home/Login/index');
        }else{
            $ses = $this->pub->get_session();
        }
        $link = $this->pub->link();
        $book_data = $this->pub->book_data($ses['id']);
        if ($book_data){
            return view ('user/user',[
                'ses'=>$ses,
                'link'=>$link,
                'book_data'=>$book_data,
            ]);
        }else{
            return view ('user/index111',[
                'ses'=>$ses,
                'link'=>$link,
            ]);
        }

    }

    public function user_vip()
    {
        if (!Session::get('user')) {
            $this->success('抱歉请先登录','home/Login/index');
        }else{
            $ses = $this->pub->get_session();
        }
        $link = $this->pub->link();
        return view('user/user_vip',[
            'ses'=>$ses,
            'link'=>$link,
        ]);

    }

    public function xiugai()
    {
        if (!Session::get('user')) {
            $this->success('抱歉请先登录','home/Login/index');
        }else{
            $ses = $this->pub->get_session();
        }
        $link = $this->pub->link();
        return view('user/xiugai',[
            'ses'=>$ses,
            'link'=>$link,
        ]);
    }

    public function do_xiugai()
    {
        $user_data = $_POST;
        $file = request()->file('image');
        if($file){
            $info = $file->validate(['size'=>222222,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $user_data['icon'] = $info->getSaveName();
            }else{
                echo $file->getError();
            }
        }

        if(!$user_data['tel']){
            $this->error('请输入电话');
        }

        if($user_data['pass'] != $user_data['repass']){
            $this->error('两次密码输入不一样');
        }
        if(!$user_data['pass']){
            unset ($user_data['pass']);
        }else{
            $user_data['pass'] = md5($user_data['pass']);
        }
        unset ($user_data['repass']);

        $res = Db::table('mh_in_user')->where(["id"=>$user_data['id']])->update($user_data);
        if($res){
            $this->success('修改成功', 'mobile/index/xiugai');
        } else {
            $this->error('修改失败');
        }


    }

    public function shangchuan()
    {
        if (!Session::get('user')) {
            $this->success('抱歉请先登录','home/Login/index');
        }else{
            $ses = $this->pub->get_session();
        }
        $link = $this->pub->link();
        $aaa = $this->pub->writer_user($ses['id']);
        $bbb = $this->pub->writer_apply($ses['id']);
        if ($aaa){
//            是作家
            return view('user/shangchuan',[
                'ses'=>$ses,
                'link'=>$link,
            ]);
        }else{
            if ($bbb){
//                申请中的作家
                return view('user/quanxian_2',[
                    'ses'=>$ses,
                    'link'=>$link,
                ]);
            }else{
//                不是作家
                return view('user/quanxian_1',[
                    'ses'=>$ses,
                    'link'=>$link,
                ]);
            }
        }
    }

    public function do_shangchuan()
    {
        if (!Session::get('user')) {
            $this->success('抱歉请先登录','home/Login/index');
        }else{
            $ses = $this->pub->get_session();
        }

        $xxx =$_POST;
        $img =[] ;
        $xxx['author'] = $ses['name'] ;
        $file = request()->file('pic');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $xxx['pic'] =  $info->getSaveName();
            }else{
                echo $file->getError();
            }
        }

        $files = request()->file('image');
        foreach($files as $file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'comic');
            if($info){
                $lll = $info->getSaveName();
                $lll = str_replace('\\','/',$lll);
                $img[] =  $lll;
            }else{
                echo $file->getError();
            }
        };

        if (!$img) {
            $this->error('请输入图片');
        }
        if (!array_key_exists('pic',$xxx)) {
            $this->error('请输入封面');
        }
        // 先查看名字   如果有就不上传名字
        $res = Db::table('mh_zuopin')->where(['author'=>$ses['name'],'name'=>$xxx['name']])->find();
        if ($res) {
            //    为真 说明 zuopin表里已经存在   不上传名字直接上传目录
            $aaa = Db::table('mh_mulu')->where(['zid'=>$res['id'],'name'=>$xxx['mulu_name']])->find();
            if ($aaa) {
                $this->error('章节名重复，请修改');
            }
            $time = time();
            $rel_mulu = Db::table('mh_mulu')->insert(['zid'=>$res['id'],'name'=>$xxx['mulu_name'],'uptime'=>$time]);
            // 在目录中上传图片
            $mid = Db::table('mh_zuopin')->getLastInsID();
            foreach ($img as $k => $v) {
                $rel_tupian = Db::table('mh_mcontent')->insert(['mid'=>$mid,'detail'=>$v]);
            }
            if($rel_tupian){
                $this->success('上传成功', 'home/index/shangchuan');
            } else {
                $this->error('上传失败');
            }
        }else{
            //    为假  说明作品表里不存在   先上传名字
            $time = time();
            $rel = Db::table('mh_zuopin')->insert(['name'=>$xxx['name'],'author'=>$xxx['author'],'detail'=>$xxx['detail'],'status'=>$xxx['status'],'pic'=>$xxx['pic'],'fid'=>1,'createtime'=>$time]);
            // 得到作品id  然后 上传目录
            $zid = Db::table('mh_zuopin')->getLastInsID();
            $aaa = Db::table('mh_mulu')->where(['zid'=>$zid,'name'=>$xxx['mulu_name']])->find();
            if ($aaa) {
                $this->error('章节名重复，请修改');
            }
            $rel_mulu = Db::table('mh_mulu')->insert(['zid'=>$zid,'name'=>$xxx['mulu_name'],'uptime'=>$time]);
            // 在目录中上传图片
            $mid = Db::table('mh_zuopin')->getLastInsID();
            foreach ($img as $k => $v) {
                $rel_tupian = Db::table('mh_mcontent')->insert(['mid'=>$mid,'detail'=>$v]);
            }
            if($rel_tupian){
                $this->success('上传成功','home/index/shangchuan');
            } else {
                $this->error('上传失败');
            }
        }
    }

    public function quanxian_1()
    {
        $ses =$this->pub->get_session();
        $res = $this->pub->do_writer_apply($ses['id'],$ses['name']);
        if ($res){
            $this->success('申请成功，请耐心等待','mobile_shangchuan');
        }else{
            $this->error('申请失败，请稍后重试');
        }
    }

    public function xiangqing()
    {
        $id = 1;
        $user_book_data = [];
        if (!Session::get('user')) {
            $this->success('抱歉请先登录','home/Login/index');
        }else{
            $ses = $this->pub->get_session();
        };
        $link = $this->pub->link();
        $comic = Db::table('mh_zuopin')->where(['id'=>$id])->find();
        $user_book = Db::table('mh_user_book')->where(['user_id'=>$ses['id']])->select();
        foreach ($user_book as $v=>$k){
            $user_book_data [] = $k['book_id'];
        }
        $aaa = in_array($id,$user_book_data);
        if ($comic['vip'] == 1){
//            免费漫画，是个人 都可以看
            return view('xiangqing/xiangqing',[
                'ses'=>$ses,
                'link'=>$link,
                'aaa'=>$aaa,
                'id'=>$id
            ]);
        }elseif($comic['vip'] == 2){
            if($ses['vip'] == 1){
//                普通用户 进入 付费漫画 失败跳转 vip购买页面
                return view('xiangqing/xiangqing',[
                    'ses'=>$ses,
                    'link'=>$link
                ]);
            }elseif($ses['vip'] == 2){
//                会员用户 进入付费漫画成功
                return view('xiangqing/xiangqing',[
                    'ses'=>$ses,
                    'link'=>$link,
                    'aaa'=>$aaa,
                    'id'=>$id
                ]);
            }
        }
    }

}
